package com.ruoyi.common.utils;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.dto.LongSearchDto;
import com.ruoyi.common.dto.ShortSearchDto;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.http.OkHttpUtils;
import okhttp3.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class PachongTool {


    public static String getDataLong(LongSearchDto longSearchDto,String cookie){
        String jsonParam = JSONObject.toJSONString(longSearchDto);
        String token = JSONObject.parseObject(cookie).getString("Token");
        if (StringUtils.isEmpty(token)) {
            throw new ServiceException("cookie参数错误");
        }
        Map<String,String> header = new HashMap<>();
        header.put("Cookie",cookie);
        header.put("kpf","PC_WEB");
        header.put("KS-CSRF-Token",token);
        header.put("Origin","https://s.kwaixiaodian.com");
        header.put("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36");
        String longUrl = "https://s.kwaixiaodian.com/rest/app/tts/ks/seller/order/query/v2";
        return OkHttpUtils.getInstance().doPostJson(longUrl,jsonParam,header);
    }

    /**
     * 根据短cookie 获取订单数据
     * @param cookie 短cookie
     * @param begin  开始时间
     * @param end    结束时间
     * @param page   页码
     * @param pageSize 每页数目
     * @param pageSize 订单状态 3-已发货  9-交易完成
     * @return
     */
    public static String getDataShort(String cookie,Long begin,Long end,int page,int pageSize,Integer customStatus) {
        ShortSearchDto shortSearchDto = new ShortSearchDto();
        shortSearchDto.setPageNo(page);
        shortSearchDto.setPageSize(pageSize);
        shortSearchDto.setUpdateStart(begin);
        shortSearchDto.setUpdateEnd(end);
        shortSearchDto.setCustomStatus(customStatus);
        String jsonParam = JSONObject.toJSONString(shortSearchDto);
        String token = new JSONObject(StringUtils.str2Map(cookie)).getString("token");
        if (StringUtils.isEmpty(token)) {
            throw new ServiceException("cookie参数错误");
        }
        Map<String,String> header = new HashMap<>();
//        header.put("Accept","application/json, text/plain, */*");
//        header.put("Accept-Encoding","gzip, deflate, br");
//        header.put("Accept-Language","zh-CN,zh;q=0.9");
//        header.put("Cache-Control","no-cache");
//        header.put("Connection","keep-alive");
        header.put("Content-Type","application/json");
        header.put("Cookie",cookie);
//        header.put("Host","ks.gd.oopaoo.com");
//        header.put("Origin","https://ks.gd.oopaoo.com");
//        header.put("Referer","https://ks.gd.oopaoo.com/orderPrint");
        header.put("token",token);
//        header.put("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36");
        String shortUrl = "https://ks.gd.oopaoo.com/api/order/list";
        String result = OkHttpUtils.getInstance().doPostJson(shortUrl,jsonParam,header);

//        OkHttpClient client = new OkHttpClient().newBuilder()
//                .build();
//        MediaType mediaType = MediaType.parse("application/json");
//        RequestBody body = RequestBody.create(mediaType, "{\"pageNo\":1,\"pageSize\":20,\"total\":0,\"updateStart\":\"1668009600000\",\"updateEnd\":\"1675871999999\",\"startPayTime\":\"\",\"endPayTime\":\"\",\"afterSaleStatus\":\"\",\"distrStatus\":\"\",\"customStatus\":\"3\",\"outSkuId\":\"\",\"skuSpec\":\"\",\"productName\":\"\",\"sellerWords\":\"\",\"keyword\":\"\",\"sortField\":\"\",\"sortWay\":\"\",\"multiOrder\":0}");
//        Request request = new Request.Builder()
//                .url("https://ks.gd.oopaoo.com/api/order/list")
//                .method("POST", body)
//                .addHeader("Cookie", "token=5944611feb59471ba617552266eb5cab")
//                .addHeader("token", "5944611feb59471ba617552266eb5cab")
//                .addHeader("Content-Type", "application/json")
//                .build();
//        Response response = null;
//        try {
//            response = client.newCall(request).execute();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        String result = null;
//        try {
//            result = response.body().string();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        return result;
    }

    public static String getExpressName(String expressNo){
//        String result = OkHttpUtils.getInstance().doGet("https://express.baidu.com/express/api/express?query_from_srcid=4001&tokenV2=5k_B76SzoBQroMkb7UGdcXYvDujDFqF2k1uCUDkeUJLrl40eJL1iWmeAHNMTroiY&nu="+expressNo);
//        if (StringUtils.isNotEmpty(result)) {
//            JSONObject jsonObject = JSONObject.parseObject(result);
//            return jsonObject.getJSONObject("company").getString("fullname");
//        } else {
//            return "查询失败";
//        }

        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
//        MediaType mediaType = MediaType.parse("text/plain");
//        RequestBody body = RequestBody.create(mediaType, "");
        Request request = new Request.Builder()
                .url("https://express.baidu.com/express/api/express?query_from_srcid=4001&tokenV2=5k_B76SzoBQroMkb7UGdcXYvDujDFqF2k1uCUDkeUJLrl40eJL1iWmeAHNMTroiY&nu=YT8495345647366")
                .method("GET",null)
                .addHeader("Cookie", "BAIDUID=7EE7C52F17DCD0CC371F699AAFED969D:FG=1; BIDUPSID=7EE7C52F17DCD0CC465B8ACD7B8984CB; PSTM=1675854208")
                .build();
        String result = "";
        try {
            Response response = client.newCall(request).execute();
            String responseResult = response.body().string();
            JSONObject jsonObject = JSONObject.parseObject(responseResult);
            String code = jsonObject.getString("status");
            if ("0".equals(code)) {
                result = jsonObject.getJSONObject("data").getJSONObject("company").getString("fullname");
            } else {
                return jsonObject.getString("msg");
            }

        } catch (IOException e) {
            result = "查询异常";
            e.printStackTrace();
        }
        return result;
    }
}
